package com.oba.king.controlmanage.proxyproduct;

import java.util.List;

import com.oba.base.dao.BaseDao;
import com.oba.base.interfaces.IBaseBo;
import com.oba.base.interfaces.IHsqlBuilder;
import com.oba.base.util.Util;
import com.oba.king.controlmanage.ProxyContract;
import com.oba.king.controlmanage.ProxyContractCondition;
import com.oba.king.datalib.productinfo.ProductInfo;

public class ProxyProductDetailsDao extends BaseDao {
	
	/**
	 * 查找所有的代理产品信息
	 * @return
	 */
		public IHsqlBuilder findAll() {
			return getSqlBuilder("from ProxyProductDetails ppd order by ppd.contract.contractId");
		}
		
		public IHsqlBuilder findAll(ProxyContract proxyContract) {
			IHsqlBuilder sql =getSqlBuilder("from ProxyProductDetails ppd");
			sql.add("ppd.contract = ?",proxyContract);
			sql.addend("order by ppd.contract.contractId");
			return sql;
		}
		
		
		public IHsqlBuilder findByCondition(ProxyProductCondition proxyProductCondition) {
			IHsqlBuilder sql = getSqlBuilder("from ProxyProductDetails p");
			sql.add("p.product.productName like ?", proxyProductCondition.getProductName(),"%","%");
			sql.add("p.product.productSerialId = ?", proxyProductCondition.getProductSerialId());
			sql.addend("order by p.contract.contractId");
			return sql;

		}
		
		public List<ProxyProductDetails> findDetailsList() {
			return find(findAll());
		}
		
		public List<ProxyProductDetails> findDetailsList(ProxyContract proxyContract) {
			return find(findAll(proxyContract));
		}
		
		
		public List<ProductInfo> findAllProduct(){
			return find("from ProductInfo p order by p.productName");
		}
		
		public IHsqlBuilder buildSelectProducts(List<IBaseBo> list) {
			String idsStr ="";
			if(list.size()!=0){
				idsStr = Util.idsFormatByBo(list);		
			} else {
				idsStr = "(0)";
			}
			IHsqlBuilder sql = getSqlBuilder("from ProductInfo p");
			sql.add("p.productId not in " + idsStr);
			sql.addend("order by p.productName");
			return sql;
		}
		
		public IHsqlBuilder buildSelectProducts(List<IBaseBo> list,ProxyProductCondition proxyProductCondition) {
			String idsStr ="";
			if(list.size()!=0){
				idsStr = Util.idsFormatByBo(list);		
			} else {
				idsStr = "(0)";
			}
			IHsqlBuilder sql = getSqlBuilder("from ProductInfo p");
			sql.add("p.productName like ?",proxyProductCondition.getProductName(),"%","%");
			sql.add("p.productSerialId = ?",proxyProductCondition.getProductSerialId());
			sql.add("p.productId not in " + idsStr);
			sql.addend("order by p.productName");
			return sql;
		}

}
